package com.cb.controller.admin.system;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.cb.core.controller.BaseFormController;
import com.cb.domain.model.system.AppResource;
import com.cb.domain.service.system.AppResourceManager;

/**
 * 
 * @Author author name
 * Create Time:
 * <p>Class Description Here [TODO]</p>
 */
public class AppResourceFormController extends BaseFormController {
	
	@Autowired
	private AppResourceManager appResourceManager;

	public AppResourceFormController() {
		 setCommandName("appResource");
	     setCommandClass(AppResource.class);
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		AppResource appResource=(AppResource)command;
		String msg=null;
		//validate the logic business data here [TODO]
		if(appResource.getResourceId()==null){
			msg=getText("appResource.added",appResource.getResourceId());
		}else{
			msg=getText("appResource.updated",appResource.getResourceId());
		}
		
		appResourceManager.save(appResource);
		
		saveMessage(request,msg);
		
		return new ModelAndView(getSuccessView());
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		AppResource appResource=null;
		String resourceId=request.getParameter("resourceId");
		if(StringUtils.isNotEmpty(resourceId)){
			appResource=appResourceManager.get(new Long(resourceId));
		}else{
			appResource=new AppResource();
		}
		return appResource;
	}
	
}
